package cn.itsource.controller;

import cn.itsource.doc.CourseDoc;
import cn.itsource.dto.CourseQueryDto;
import cn.itsource.repo.CourseElasticsearchRepository;
import cn.itsource.result.JSONResult;
import cn.itsource.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Jason
 * @Date: 2022/9/4 18:44
 */
@RequestMapping("/es/course")
@RestController
public class CourseEsController {

    @Autowired
    private CourseElasticsearchRepository repository;

    @Autowired
    private EsService esService;

    @PostMapping("/searchCourse")
    public JSONResult searchCourse(@RequestBody CourseQueryDto dto){
        return esService.searchCourse(dto);
    }

    //需要上线的课程信息存ES
    @PostMapping("/saveCourse")
    @PreAuthorize("hasAuthority('es:saveCourse')")
    public JSONResult saveCourse(@RequestBody List<CourseDoc> doc){
        repository.saveAll(doc);
        return JSONResult.success();
    }

    //需要上线的课程信息存ES
    @PostMapping("/deleteCourse")
    public JSONResult deleteCourse(@RequestBody List<CourseDoc> doc){
        repository.deleteAll(doc);
        return JSONResult.success();
    }
}
